package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shopping_08_20/api/request"
	"shopping_08_20/internal/model"
)

func AddShop(c *gin.Context) {
	var req request.AddShopForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var shop model.Shop

	shop = model.Shop{
		Name:   req.Name,
		Info:   req.Info,
		Pricer: req.Pricer,
	}

	err := shop.AddShop()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "添加失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "添加成功",
		"data":    shop,
	})

}

func IdGetShop(c *gin.Context) {
	var req request.IdGetShopForm

	err := c.ShouldBindJSON(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var shop model.Shop

	shop.Id = req.Id

	err = shop.IdGetShop()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "查找失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查找成功",
		"data":    shop,
	})
}

func UpdateShop(c *gin.Context) {
	var req request.UpdateShopForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var shop model.Shop

	shop = model.Shop{
		Id:     req.Id,
		Name:   req.Name,
		Info:   req.Info,
		Pricer: req.Pricer,
	}

	err := shop.UpdateShop()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "商品信息修改失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "商品信息修改成功",
		"data":    shop,
	})

}

func DeleteShop(c *gin.Context) {
	var req request.DeleteShopForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var shop model.Shop

	shop.Id = req.Id

	err := shop.DeleteShop()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "商品条目删除失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "商品条目删除成功",
		"data":    "",
	})
}
